import { getList, findVehicleDetail, findHistory, findWheelPath, exportVehicleHistory,
  activeApply, findVehicleApply, addBatchVehicleActiveApply,
  searchTboxGMDSS, vehicleLogout, bindingTbox, bindingPdsn,
  add, edit, findProfile, findTboxSmsBySmsId, simulateDownTboxLogs,
  simulateUpTboxLogs, getEnrolledVehicle, resendActive, searchDMTboxGMDSS,
  findVerificationCodeByVin, checkVerifCode,getEnterList,
  reAuth, authAndActivation, getAuthInfo, searchDMTboxBusiGMDSS,
  searchTboxBusiGMDSS, reSendActiveInfo, vehicleRelieve, vehicleFactoryLogout,
  searchVehicleBaic, searchVehicleBaicDealer, getVehicleInstrument, commonGet,
  getIccid,queryList,
 } from '@/services/vehicle';

import * as R from 'ramda'
import { successCode } from '@/support/constant/common'
import * as G from '../utils/common'

const detail = { detail: {} }
const status = { status: {} }
const flow = { flow: {} }
const user = { user: {}, contract: '', contractPhone: '' }

const isSuccess = R.compose(
  R.includes(R.__, successCode),
  R.prop('statusCode'),
)

// 车辆详情初始化，无论返回的数据是什么格式，保证state中是符合预期的
const dataDefault = R.curry(
  (data, response) => R.compose(
    R.mergeRight(data),
    G.omitEmptyAndNil,
    R.propOr({}, 'data'),
  )(response),
)

export default {
  namespace: 'vehicle',
  state: {
    list: {
      list: [],
      pagination: {},
    },
    dealerList: {
      list: [],
      pagination: {},
    },
    loading: false,
    detail, status, flow, user,
    tabData: {
      list: [],
      pagination: {},
    },
    listlLoading: false,
    recordList: [],
    pagination: {
      current: 1,
      pageSize: 10,
      total: 0,
    }

  },
  effects: {
    * getVehicleDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'detail',
        payload: { detail: { detail: {} } },
      })
      const response = yield call(commonGet, payload)
      if (!isSuccess(response)) {
        yield put({
          type: 'detail',
          payload: { detail: { detail: {} } },
        })
        return
      }
      const detail = dataDefault(detail, response)
      yield put({
        type: 'detail',
        payload: { detail },
      })
      if (callback) callback(response)
    },

    * getVehicleStatus({ payload, callback }, { call, put }) {
      yield put({
        type: 'detail',
        payload: { status: { status: {} } },
      })
      const response = yield call(commonGet, payload)
      if (!isSuccess(response)) {
        yield put({
          type: 'detail',
          payload: { status: { status: {} } },
        })
        return
      }
      const defaultRes = dataDefault(status, response)

      // 海马返回的字段是 hmStatus, 这里要做到数据源的统一
      const strandStatus = R.mergeRight(defaultRes.status, defaultRes.hmStatus)
      const statusLens = R.lensProp('status')
      const target = R.set(statusLens, strandStatus, response.data)

      yield put({
        type: 'detail',
        payload: { status: target },
      })
      if (callback) callback(response)
    },

    * getVehicleFlow({ payload, callback }, { call, put }) {
      yield put({
        type: 'detail',
        payload: { flow: { flow: {} } },
      })
      const response = yield call(commonGet, payload)
      if (!isSuccess(response)) {
        yield put({
          type: 'detail',
          payload: { flow: { flow: {} } },
        })
        return
      }
      const flow = dataDefault(flow, response)
      yield put({
        type: 'detail',
        payload: { flow },
      })
      if (callback) callback(response)
    },

    * getVehicleUser({ payload, callback }, { call, put }) {
      yield put({
        type: 'detail',
        payload: { user: { user: {} } },
      })
      const response = yield call(commonGet, payload)
      if (!isSuccess(response)) {
        yield put({
          type: 'detail',
          payload: { user: { user: {} } },
        })
        return
      }
      const strand = dataDefault(user, response)
      yield put({
        type: 'detail',
        payload: { user: strand },
      })
      if (callback) callback(response)
    },

    *findVehicleDetail({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVehicleDetail, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *fetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getList, payload);
      yield put({
        type: 'appendList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *enterFetch({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getEnterList, payload);
      yield put({
        type: 'appendList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *enrolledVehicle({ payload }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getEnrolledVehicle, payload);
      yield put({
        type: 'appendList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
    },
    *findHistory({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findHistory, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findWheelPath({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findWheelPath, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *exportVehicleHistory({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(exportVehicleHistory, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *activeApply({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(activeApply, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findVehicleApply({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVehicleApply, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *addBatchVehicleActiveApply({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(addBatchVehicleActiveApply, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchTboxGMDSS({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchTboxGMDSS, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *vehicleLogout({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(vehicleLogout, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *bindingTbox({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(bindingTbox, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *bindingPdsn({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(bindingPdsn, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *add({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(add, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *edit({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(edit, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findProfile({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findProfile, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findTboxSmsBySmsId({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findTboxSmsBySmsId, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *simulateDownTboxLogs({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(simulateDownTboxLogs, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *simulateUpTboxLogs({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(simulateUpTboxLogs, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *resendActive({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(resendActive, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchDMTboxGMDSS({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchDMTboxGMDSS, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *findVerificationCodeByVin({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(findVerificationCodeByVin, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *checkVerifCode({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(checkVerifCode, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *reAuth({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(reAuth, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *authAndActivation({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(authAndActivation, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *getAuthInfo({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getAuthInfo, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchDMTboxBusiGMDSS({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchDMTboxBusiGMDSS, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchTboxBusiGMDSS({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchTboxBusiGMDSS, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *reSendActiveInfo({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(reSendActiveInfo, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *vehicleRelieve({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(vehicleRelieve, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *vehicleFactoryLogout({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(vehicleFactoryLogout, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchVehicleBaic({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchVehicleBaic, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *searchVehicleBaicDealer({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(searchVehicleBaicDealer, payload);
      yield put({
        type: 'appendDealerList',
        payload: response,
      });
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    *getVehicleInstrument({ payload, callback }, { call, put }) {
      const response = yield call(getVehicleInstrument, payload);
      if (callback) callback(response);
    },
    *getIccid({ payload, callback }, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getIccid, payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      if (callback) callback(response);
    },
    //分页查询调拨历史
    *queryList ({ payload, callback }, { call, put }) {
      yield put({
        type: 'query',
        payload: { listlLoading: true },
      })
      const response = yield call(queryList, payload)
      yield put({
        type: 'query',
        payload: {
          listlLoading: false,
          recordList: response.data?.list,
          pagination: {
            current: response.data?.pager.currentPage,
            pageSize: response.data?.pager.pageSize,
            total: response.data?.pager.totalRows,
          }
        },
      })
      if (callback) callback(response);
      return response;
    },

  },

  reducers: {
    detail(state, action) {
      return {
        ...state,
        ...action.payload,
      }
    },
    save(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    appendList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    appendDealerList(state, action) {
      return {
        ...state,
        dealerList: action.payload,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
    query(state, action) {
      return {
        ...state,
        ...action.payload,
      }
    },

  },
};
